import store from '@/store/index.js';

/**
 * 产品映射库
 * */
class Products {
	constructor(datas, serverTime) {
		this.datas = datas;
		this.serverTime = serverTime.replace(/\-/g, '/');
		this.productsMap = Object.create(null); // 产品库
		this.goodsList = new Array(); // 菜单商品列表
	}

	// 处理数据
	init() {
		// 处理产品
		const originProducts = this.datas.filter(x => x.col_type !== '1');
		const productsMap = {}; // 产品库
		originProducts.map(x => {
			let products = x.products || [];
			let newProducts = [];
			products.map(y => {
				if (y.code && y.col_type != 1) {
					let {
						id,
						code,
						col_type,
						con_day_flag,
						col_font_new,
						col_font,
						col_position,
						new_type,
						bn_type,
						bn_link_url,
						name,
						alias,
						features,
						special,
						productDesc,
						shortDesc,
						price,
						vipPrice,
						specialPrice,
						boxprice,
						boxnum,
						startDay,
						endDay,
						startTime1,
						startTime2,
						endTime1,
						endTime2,
						week,
						bn_img_url,
						displayOrder,
						hd_limit_count,
						productType,
						shuxing,
						images,
						itemSets
					} = y;

					vipPrice = vipPrice || 0;
					specialPrice = specialPrice || 0;

					const currentStoreInfo = store.state.currentStoreInfo;

					const pic = images.mini;
					const max = hd_limit_count || 999;
					const status = currentStoreInfo.originalBusinessState === '2';
					let buttonType = 0;
					let basePrice = 0;
					let originPrice = price;

					buttonType = this.getButtonType(id, itemSets, shuxing, productType, displayOrder);

					if (vipPrice === 0 && specialPrice === 0) {
						basePrice = price;
					} else {
						basePrice = specialPrice || vipPrice || price;
					}
					
					let isAvailable = this.judgeIsAvailable(con_day_flag, week, startDay, endDay, startTime1, startTime2, endTime1, endTime2); // 是否可售
		
					const goodsItem = {
						id, // 产品id
						code, // 产品编码
						col_type, // 0:产品目录,1:广告目录,2:非单卖目录（前端不展示,但存在产品库中）
						con_day_flag, // ? 为1则产品不可售
						col_font_new, // 目录左侧分类图标
						col_font, // 目录icon
						col_position, // banner 位置
						new_type, // 新类型: 0 产品 1 banner
						bn_type, // banner类型: new_type为1时: 0: 仅图片，1： 图片+产品 2： 图片 + 链接 ; new_type 为0 bn_type为null
						bn_link_url, // banner 链接地址
						name: alias, // 产品名
						alias, // 产品别名
						features, // 产品特色
						special, // 热销标识
						productDesc, // 产品标签
						shortDesc, // 产品描述
						price, // 产品价格（原价） 1
						vipPrice, // 会员价 2
						specialPrice, // 特价 3
						boxprice, // 包材费
						boxnum, // 包材数量
						startDay,  // 产品开始售卖时间 YYYY-MM-DD
						endDay, // 产品结束售卖时间 YYYY-MM-DD
						startTime1, // 开始售卖时间段1 hh:mm:ss
						startTime2,
						endTime1, // 结束售卖时间段1 hh:mm:ss
						endTime2,
						week, // 周几售卖 null不限 ; 1~7
						bn_img_url, // 菜单之前最上面banner轮播图
						displayOrder, // 小于0 标识售罄
						hd_limit_count, // 最大限制购买量
						productType, // 产品类型 1为真套餐
						shuxing, // 属性
						images, // 产品所有图像
						itemSets, // 选项组分组列表
						pic, // mini产品图
						max, // 最大购买量
						status, // 门店状态
						buttonType, // 按钮类型 
						basePrice, // 基准价
						originPrice, // 原价
						isAvailable // 是否可售
					}

					productsMap[y.id] = goodsItem;
					productsMap[y.code] = goodsItem;

					newProducts.push(goodsItem);
				}
			});
			
			x.products = newProducts;
		});

		this.productsMap = productsMap;

		return originProducts;
	}

	// 根据时间判断是否可售
	judgeIsAvailable(con_day_flag, week, startDay, endDay, startTime1, startTime2, endTime1, endTime2) {
		if (con_day_flag === '1') return false;

		let curWeek = (new Date(this.serverTime)).getDay(); // 当前星期几
		if (curWeek === 0) curWeek = 7;

		if (week) {
			if (week.indexOf(curWeek) === -1) {
				return false;
			}
		}

		const _serverTime = Number(new Date(this.serverTime)); // 当前时间戳
		if (startDay && endDay) {
			startDay = (startDay + ' 00:00:00').replace(/\-/g, '/');
			endDay = (endDay + ' 23:59:59').replace(/\-/g, '/');

			const _startDay = Number(new Date(startDay));
			const _endDay = Number(new Date(endDay));

			if (_serverTime < _startDay || _startDay > _endDay) {
				return false;
			}
		}
		
		let timeQuantum = 0; // 时间段  
		if (startTime1 && endTime1 && startTime2 && endTime2) {
			timeQuantum = 2;
		} else if(startTime1 && endTime1) {
			timeQuantum = 1;
		} else {
			timeQuantum = 0;
		}
		
		const curDate = this.serverTime.slice(0, 10); // 当前年月日
		if (timeQuantum !== 0) {
			const _startTime1 = Number(new Date(`${curDate} ${startTime1}`));
			const _endTime1 = Number(new Date(`${curDate} ${endTime1}`));
			
			if (timeQuantum === 1) {
				if (_serverTime < _startTime1 || _startDay > _endTime1) {
					return false;
				}
			}
			
			if (timeQuantum === 2) {
				const _startTime2 = Number(new Date(`${curDate} ${startTime2}`));
				const _endTime2 = Number(new Date(`${curDate} ${endTime2}`));
				
				if (_serverTime < _startTime1 || _serverTime > _endTime2 || (_serverTime > _endTime2 && _serverTime < _startTime2)) {
					return false;
				}
			}

		}
		

		return true;
	}


	/**
	 * 按钮类型  只在此组件做判断用
	 * 1：纯单品      !itemSets.length && (!shuxing || !shuxing.length)  显示加减号
	 * 2：单品选搭配  与纯单品判断一致，但能在查找关联库的找到它的产品id    显示选搭配按钮
	 * 3：非套餐单品	 itemSets.length && productType == 0    显示多个按钮
	 * 4：套餐        itemSets.length && productType == 1   显示选规格按钮
	 * 5: 售罄		 displayOrder < 0
	 * */
	getButtonType(id, itemSets, shuxing, productType, displayOrder) {
		const relevanceMap = store.state.relevanceMap;
		let buttonType = 0;
		if (!itemSets.length) {
			if (!shuxing && !relevanceMap[`${id}_2_1`] && !relevanceMap[`${id}_1_1`]) {
				buttonType = 1;
			} else {
				buttonType = 2;
			}
		} else {
			if (productType === '0') {
				buttonType = 3;
			} else {
				buttonType = 4;
			}
		}
		if (displayOrder < 0) {
			buttonType = 5;
		}

		return buttonType;
	}

	// 获取产品库
	getMap() {
		return this.productsMap;
	}

}

module.exports = Products;
